package cn.org.timespace.easyframe.util.sms;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.org.timespace.easyframe.util.EncryptUtil;
import cn.org.timespace.easyframe.util.SMGPPropertie;
import cn.org.timespace.easyframe.util.sms.telecom.SendReceiveInterface;

/**
 * Created by IntelliJ IDEA. User: wubo Date: 2003-12-4 Time: 23:33:13 To change
 * this template use Options | File Templates.
 */

public class SmsManager {
	private static final Log log =LogFactory.getLog(SmsManager.class);
	
	private static SmsManager _instance = null;

	public static SmsManager getInstance() {
		if (_instance == null)
			_instance = new SmsManager();
		return _instance;
	}

	/**
	 * ���Ʒ��Ͷ���
	 * 
	 * @param String
	 *            mobile notNull �绰���� ���绰ʱ�� ","�ָ�
	 *            
	 * @return  '0':���ŷ��ͳɹ�      '-3':���ŷ���ʧ��   '-4':��¼�������ʧ�ܵ��¶��ŷ���ʧ�ܣ�    '-5':��¼����쳣��            
	 * */
	public static String sendSmsManager(String mobile, String content) {
		
		int sendType = Integer.parseInt(SMGPPropertie.getProperty(
				"smgp.sendType","0").trim());// ���ŷ�������
		log.info("���Ʒ��Ͷ��� sendType:"+sendType);
		String result = "0";
		
		switch (sendType) {
		case 0:
			result = sendSmsTelecomMsg(mobile, content);//���ŷ���
			break;
		case 1:
			result = sendSmsWebserviceMsg(mobile, content);//����AP����
			break;
		case 2:
			result = sendSmsUnicomMsg(mobile,content);//����jͨ����
			break;
		case 3:
			result = UnicomMessage.send(mobile, content);//����jͨ����
			break;
		case 4:
			//�����ƶ�
			result = MobileMessage.send(mobile, content);
			break;
		case 5:
			//���� ����ƽ̨
			Map<Integer, String> map= SMGPPropertie.dividePhoneType(mobile);
			
			String telecomStr = map.get(0);
			String mobileStr = map.get(1);
			String unicomStr = map.get(2);
			
			if(telecomStr!=null&&telecomStr.length()>0){
				result = sendSmsWebserviceMsg(telecomStr, content);//����AP����
			}
			if(!"0".equals(result))break;
			if(mobileStr!=null&&mobileStr.length()>0){
				result = MobileMessage.send(mobileStr, content);//�����ƶ�
			}
			if(!"0".equals(result))break;
			if(unicomStr!=null&&unicomStr.length()>0){
				
				String[] s1=unicomStr.split(",");
				if(s1.length<=100){
					result = UnicomMessage.send(unicomStr, content);//����jͨ����
				}else{
					int i=s1.length/100;
					int j=s1.length%100;
					for(int a=0;a<i;a++){
						String s2="";
						for(int b=a*100;b<(a+1)*100;b++){
							s2+=s1[b]+",";
						}
						result = UnicomMessage.send(s2, content);//����jͨ����
					}
					if(j>0){
						String s2="";
						for(int b=i*100;b<s1.length;b++){
							s2+=s1[b]+",";
						}
						result = UnicomMessage.send(s2, content);//����jͨ����
					}
				}
			}
			break;
		}

		return result;
	}

	private static String sendSmsTelecomMsg(String mobile, String content) {
		String[] userNumber = mobile.split(",");
		return send(userNumber, content);
	}

	/**
	 * ���� ���� ƽ̨ ����
	 * */
	public synchronized static String sendSmsWebserviceMsg(String mobile, String content) {
		log.info("���� ���� ƽ̨ ����");
		String url = SMGPPropertie.getProperty("smgp.url").trim();// ƽ̨�����http://58.53.194.80/swdx/services/SmsBizService?wsdl
		String apName = SMGPPropertie.getProperty("smgp.apName").trim();// SP�ʺ�wanzongfff
		String apPass = SMGPPropertie.getProperty("smgp.apPassword").trim();// SP����111111
		String timestampLong = System.currentTimeMillis()+"";
		apPass=EncryptUtil.getEncodedPassword(apName+apPass+timestampLong);//MD5����
		String compId = SMGPPropertie.getProperty("smgp.compcode").trim();// ��ҵ��ʶwanzong
		String id = SMGPPropertie.getProperty("smgp.userCode").trim();// ��ҵ�ʺ�wanzong
		String accountId = SMGPPropertie.getProperty("smgp.accountId").trim();// ��ҵ�ʺ�wanzong
		
		String status="1";
		
		
		return status;
	}
	
	//����ʧ��ʱ���ظ��������(jͨ)������ɹ���嵽��ݿ���ȥ�������ˡ�
	private static String sendSmsUnicomMsg(String mobile,String content){
		Send send=new Send();
    	String result="";
    	
    	String[] userNumber=mobile.split(",");
		Integer isSuccess=send(send, userNumber,content);
		
		//��isSuccess��=0��ʱ���ظ�������Σ�����ɹ���嵽��ݿ���ȥ�������ˡ�
		for(int i=3;i>0&&isSuccess!=0;i--){
			isSuccess=send(send, userNumber,content);
		}
		//���¸ö��ŵ�״̬
		if(isSuccess==0){
			result="0";
		}else{
			result="-3";
		}
		
    	return result;
	}

	/**
	 * ���Ͷ��� ֱ�ӷ��͸�jͨ���
	 * */
	public static int send(Send send, String[] userNumber, String content) {
		
		int isSuccess = -3;
		try {
			isSuccess = send.sendSms(userNumber, content);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			isSuccess = -3;
		}
		return isSuccess;
	}

	/**
	 * ���Ͷ��� ֱ�ӷ��͸�������
	 * 
	 * @param String
	 *            [] userPhone notNull Ŀ�����
	 * @param Stringc
	 *            content Null ��������
	 *            
	 * @return  '0':���ŷ��ͳɹ�      '-3':���ŷ���ʧ��   '-4':��¼�������ʧ�ܵ��¶��ŷ���ʧ�ܣ�    '-5':��¼����쳣��           
	 * */
	public static String send(String[] userPhone, String content) {
		String status = "0";
		try {
			SendReceiveInterface inter = SendReceiveInterface.getInstance();
			if (inter.proxyStatus == 0) {
				boolean result = inter.Send(userPhone, content);
				if (result) {
					//saveSendMsg(userPhone, content, "0");
					log.info("���ŷ��ͳɹ���");
					status = "0";
				} else {
//					saveSendMsg(userPhone, content, "-3");
					log.info("���ŷ���ʧ�ܣ�status:-3 ");
					status = "-3";
				}
			} else {
				//saveSendMsg(userPhone, content, "-4");
				log.info("��¼�������ʧ�ܵ��¶��ŷ���ʧ�ܣ�status:-4 ");
				status = "-4";
			}
		} catch (Exception e) {
			log.error("��¼��������쳣��status:-5 error:"+e.getMessage());
			status = "-5";
		}
		return status;
	}

	/**
	 * ���淢�͵Ķ���
	 * */
	private static void saveSendMsg(String[] userPhone, String content,
			String result) {
		String srcTermId = SMGPPropertie.getProperty("smgp.srcTermId");// ����Ϣ�����û�����
		int destLen = userPhone.length;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
	}
}